package jianger.utils;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author jianger
 * @date 2018/2/25
 * <p>
 * 使用动态代理ip池处理
 */
public class GetDocumentUtils {

    private static List<String> ipList = new ArrayList<String>();

    static {
        // 获取代理ip
        BufferedReader ipData = null;
        try {
            ipData = new BufferedReader(new FileReader(new File("H:\\Git\\" +
                    "jdspider\\src\\main\\resources\\ipData.txt")));
            String line;
            while ((line = ipData.readLine()) != null) {
                ipList.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Document getDecument(String url) throws IOException {
        Random random = new Random();
        int anInt = random.nextInt(ipList.size());
        String ips = ipList.get(anInt);
        String[] strings = ips.split(":");
        //使用代理ip
        HttpHost proxy = new HttpHost(strings[0], Integer.parseInt(strings[1]));
        ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(4128).build();
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        CloseableHttpClient hc = HttpClients.custom().setDefaultConnectionConfig(connectionConfig)
                .setRoutePlanner(routePlanner).build();


        // 1. 获取一个httpGet对象
        HttpGet httpGet = new HttpGet(url);
        // 2.获取一个httpclient对象，用来执行http请求
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 3.执行请求，并得到返回对象
        CloseableHttpResponse response = httpclient.execute(httpGet);
        Document document = null;
        if (response.getStatusLine().getStatusCode() == 200) {
            // 4. 返回一个html
            String html = EntityUtils.toString(response.getEntity(), Charset.forName("gbk"));
            // 5. jsoup解析文档
            document = Jsoup.parse(html);
        }
        return document;
    }

    public static void main(String[] args) throws IOException {
        Document decument = GetDocumentUtils.getDecument("https://www.baidu.com/");
        System.out.println(decument);


    }

}

